#include <stdio.h>
#include <stdlib.h>
#include <sys/select.h>
#include <fcntl.h>
#include <unistd.h>

int main() {
    int fd;
    int fd1;
    fd_set read_fds;
    struct timeval timeout;

    fd = open("/dev/char_driver", O_RDONLY);
    if (fd < 0) {
        perror("Failed to open /dev/char_driver");
        exit(EXIT_FAILURE);
    }

    fd1 = open("/dev/char_driver", O_RDONLY);
    if (fd < 0) {
        perror("Failed to open /dev/char_driver");
        exit(EXIT_FAILURE);
    }   

    printf("fd: %d fd1: %d\n", fd, fd1);

    while (1) {
        FD_ZERO(&read_fds);
        FD_SET(fd, &read_fds);
        FD_SET(fd1, &read_fds);

        timeout.tv_sec = 5; // 设置超时时间为5秒
        timeout.tv_usec = 0;

        int ret = select(fd1 + 1, &read_fds, NULL, NULL, &timeout);
        if (ret < 0) {
            perror("select error");
            exit(EXIT_FAILURE);
        } else if (ret == 0) {
            printf("Timeout reached. No data available.\n");
        } else {
            if (FD_ISSET(fd, &read_fds)) {
                printf("/dev/char_driver is readable. Data available.\n");
                // 在这里可以读取设备文件内容或执行其他操作
            }

            if (FD_ISSET(fd1, &read_fds)) {
                printf("/dev/char_driver is readable. Data available. 1111\n");
                // 在这里可以读取设备文件内容或执行其他操作
            }
        }
    }

    close(fd);
    return 0;
}

